<?php
// +----------------------------------------------------------------------
// | When work is a pleasure, life is a joy!
// +----------------------------------------------------------------------
// |  User: ShouKun Liu  |  Email:24147287@qq.com  | Time:2017/3/9 15:18
// +----------------------------------------------------------------------
// | TITLE: 测试的基础验证
// +----------------------------------------------------------------------
namespace app\first\auth;

use app\common\controller\Jwt;
use app\first\model\Member;
use DawnApi\auth\Basic;
use function PHPSTORM_META\type;
use think\Exception;
use think\facade\Config;
use think\facade\Response;
use think\Request;
use DawnApi\exception\UnauthorizedException;

class BasicAuth extends Basic{
    public $uid;
    public $member = [];
    /**
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function certification(Request $request){
      return $this->getClient($request)->getUser();
    }

    /**
     * @param Request $request
     * @return bool|UnauthorizedException|\Exception|object|Exception|\think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \Exception
     */
    public function authenticate(Request $request){
        return $this->getClient($request);
    }

    /**
     * @return array|mixed
     * @throws UnauthorizedException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUser(){
        $member =  Member::getMember($this->uid);
        if(!$member){
            throw new UnauthorizedException(lang('authentication'));
        }
        return $member->toArray();
    }

    public function getUser1(){
        $member =  Member::getMember([
            ['username','eq',$this->username],
            ['password','eq',$this->_password($this->password)],
        ]);
        $this->member = $member->toArray();
        return $this;
    }

    protected function setUid(){
        $this->uid = $this->member['user_id'];
    }

    /**
     * 获取客户端
     * @param Request $request
     * @return bool|object|void
     * @throws UnauthorizedException
     */
    public function getClient(Request $request){
        $authorization = $request->header('authorization');
        if (strpos($authorization,'Bearer')!==false) {
            $authorization = str_replace('Bearer ', '', $authorization);
        }
        //p(gettype($authorization));
       // p(strlen($authorization));
        $key = Config::get('app.ace_key');
        $jwt = Jwt::decode($authorization,$key,$alg = array('HS256'));
        //p($jwt);die;
        if(time() > $jwt->exp){
            throw new UnauthorizedException('token已失效');
        }
        $this->uid = $jwt->uid;
        return true;
//        if($authorization){
//            if (strpos($authorization,'Basic') !==false) {
//                $authorization = str_replace('Basic ', '', $authorization);
//                $authorization = str_replace("Basic ", "", $authorization);
//                $authorization = explode(':', base64_decode($authorization));
//                $this->username = $authorization[0];
//                $this->password = $authorization[1];
//                $this->getUser1()->setUid();
//                return true;
//            }else{
//                $authorization = str_replace('Bearer ', '', $authorization);
//                p($authorization);
//                $key = Config::get('app.ace_key');
//                $jwt = Jwt::decode($authorization,$key,$alg = array('HS256'));
//                p($jwt);die;
//                if(time() > $jwt->exp){
//                    throw new UnauthorizedException('token已失效');
//                }
//                $this->uid = $jwt->uid;
//                return true;
//            }
//        }else{
//            $headers['WWW-Authenticate'] = 'Basic realm=\"STOP!\"';
//             Response::create('','',200,$headers)->send();
//        }
    }

    private function _password($pwd){
        return substr(md5($pwd), 10, 15);
    }
}